package com.lingying.caiwudailikehudaun.utils;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
import com.aliyun.oss.model.ObjectMetadata;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author 何云鹏
 * @create 2020-04-27 19:17
 */
public class OSSUtil {
    //外网访问节点
    private static String endpoint = "oss-cn-beijing.aliyuncs.com";
    private static String accessKeyId = "LTAI4G84DzpVdV4gBZSx9Wvg";
    private static String accessKeySecret = "MqwFBDvuNlTL4kG9zZTfSetr8Nk6MG";
    //仓库名称
    private static String bucketName = "lingyingsms";
    //上传成功后返回的地址
    private static String suffer_url = "http://lingyingsms.oss-cn-beijing.aliyuncs.com/";
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    //获取oss连接
    public OSSClient getOSSClient() {
        //创建一个OSSClient 对象
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        //判断仓库是否存在
        if (ossClient.doesBucketExist(bucketName)) {
            System.out.println("bucket创建成功");
        } else {//通过api接口形式创建bucket仓库
            System.out.println("bucket不存在，创建bucken:" + bucketName);
            CreateBucketRequest bucketRequest = new CreateBucketRequest(null);
            bucketRequest.setBucketName(bucketName);//设置仓库名称
            bucketRequest.setCannedACL(CannedAccessControlList.PublicRead);//设置权限为公共读
            ossClient.createBucket(bucketRequest);
            ossClient.shutdown();
        }
        return (OSSClient) ossClient;
    }

    /**
     * 上传文件
     *
     * @param multipartFile 上传的文件
     * @param businessType  上传文件类型
     * @return
     */
    public String uploadDocument(MultipartFile multipartFile, String businessType) {
        System.out.println(businessType);
        //获取oss连接
        OSSClient ossClient = this.getOSSClient();
        //获取文件的后缀名称
        String suffixName = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));

        //上传至oss的那个文件夹通过fileName 来指定
        String date = sdf.format(new Date());
        if (StringUtils.isEmpty(businessType)) {
            businessType = "default";
            System.out.println(businessType);
        }
        //避免图片重名，使用uuid来命名图片
        String uuid = UUID.randomUUID().toString().replace("-", "");
        //组合filemame
        String fileName ="sms/"+businessType+"/"+date + "/" + uuid + suffixName;
        String url = null;

        try {
            //设置在线预览
            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setContentType("image/jpg");
            objectMetadata.setContentType("image/png");
            objectMetadata.setContentType("image/jpeg");
            objectMetadata.setContentType("audio/mpeg");
            objectMetadata.setContentType("audio/mp4");
            objectMetadata.setContentType("audio/x-m4a");
            objectMetadata.setContentType("audio/x-ms-wma");
            objectMetadata.setContentType("audio/zip");
            //通过ossclient来获取上传文件后返回的url
            ossClient.putObject(bucketName, fileName, new ByteArrayInputStream(multipartFile.getBytes()),objectMetadata);
            url = suffer_url + fileName;
            System.out.println("上传成功"+url);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            ossClient.shutdown();
        }
        return url;
    }
}
